import * as types from '../actionTypes/portal';
import update from "immutability-helper";
import session from 'src/utils/session';

const initialState = {
    currentPane: "home",
    panes: [{
        key: 'home',
        title: '首页',
        label: '首页',
        closable: false
    }],
    menus: [
        {
            key: 'home',
            title: "首页",
            label: '首页',
            path: 'home',
        },
        {
            key: 'sub1',
            title: "数据管理",
            label: '数据管理',
            children: [
                {
                    key: 'userlist',
                    title: '用户列表',
                    label: '用户列表',
                    path: 'userlist',
                },
                {
                    key: 'shoplist',
                    title: '商家列表',
                    label: '商家列表',
                    path: 'shoplist',
                },
                {
                    key: 'foodlist',
                    title: '食品列表',
                    label: '食品列表',
                    path: 'foodlist',
                },
                {
                    key: 'orderlist',
                    title: '订单列表',
                    label: '订单列表',
                    path: 'orderlist',
                },
                {
                    key: 'adminlist',
                    title: '管理员列表',
                    label: '管理员列表',
                    path: 'adminlist',
                },
            ]
        },
        {
            key: 'sub2',
            title: '添加数据',
            label: '添加数据',
            children: [
                {
                    key: "addshop",
                    title: '添加商铺',
                    label: '添加商铺',
                    path: 'addshop',
                },
                {
                    key: "addgoods",
                    title: "添加商品",
                    label: '添加商品',
                    path: 'addgoods',
                }
            ]
        },
        {
            key: "sub3",
            title: "图表",
            label: '图表',
            children: [
                {
                    key: "visitor",
                    title: "用户分布",
                    label: '用户分布',
                    path: 'visitor',
                }
            ]
        },
        {
            key: "sub4",
            title: "编辑",
            label: '编辑',
            children: [
                {
                    key: "edit",
                    title: "文本编辑",
                    label: '文本编辑',
                    path: 'edit',
                },
            ]
        },
        {
            key: "sub5",
            title: "设置",
            label: '设置',
            children: [
                {
                    key: "adminSet",
                    title: "管理员设置",
                    label: '管理员设置',
                    path: 'adminSet',
                }
            ]
        },
        {
            key: "sub6",
            title: "关于",
            label: '关于',
            children: [
                {
                    key: "about",
                    title: "关于",
                    label: '关于',
                    path: 'about',
                }
            ]
        },
        {
            key: "sub7",
            title: "实验室",
            label: '实验室',
            children: [
                {
                    key: "setting",
                    title: "系统设置",
                    label: '系统设置',
                    path: 'setting',
                },
                {
                    key: "upload",
                    title: "上传文件",
                    label: '上传文件',
                    path: 'upload',
                },
                {
                    key: "iframe/login",
                    title: "登录",
                    label: '登录',
                    isiframe: 'true',
                    src: '/login',
                    path: 'iframe/login',
                },
                {
                    key: "iframe/register",
                    title: "注册",
                    label: '注册',
                    isiframe: 'true',
                    src: '/register',
                    target: '_blank',
                    path: 'iframe/register',
                }
            ]
        },
    ],
    theme: 'light'
};

const reducer = (state = initialState, action) => {
    switch (action.type) {
        case types.INCREMENT_PANES: {
            session.setItem('TABS_DATA', action.payload)
            return update(state, {
                panes: { $set: action.payload }
            });
        }
        case types.CHANGE_CURRENT_PANE: {
            return update(state, {
                currentPane: { $set: action.payload.pane }
            })
        }
        case types.CHANGE_THEME: {
            return update(state, {
                theme: { $set: action.payload.theme }
            })
        }
        default: {
            let TABS_DATA = session.getItem('TABS_DATA');

            if (TABS_DATA) {
                return update(state, {
                    panes: { $set: TABS_DATA }
                });
            }
            return state;
        }
    }
}

export default reducer;